package com.nb.hdj.testCase.consultModule;
import com.nb.hdj.util.DateUtils;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;

/**
 * @Description: 发布咨询测试用例
 * @Author: hedongjia
 * @Date: 2021/11/22 上午10:33
 * @Version: v1.0
 */
public class ReleaseConsultBase extends ConsultCaseBase{
    private int releaseNum;
    /**
     * 发布咨询测试
     * @param driver
     * @return
     */
     public boolean releaseConsult(WebDriver driver){
         try {
             Thread.sleep(500);
             //选择要发布的咨询 默认新增加的第一行
             seleniumUtil.elementClick(driver,By.xpath(consultMap.get("ReleaseOptionElement")));
             Thread.sleep(500);
             //点击立即发布按钮
             seleniumUtil.elementClick(driver, By.xpath(consultMap.get("ReleaseLijiElement")));
             //点击确定
             Thread.sleep(500);
             seleniumUtil.elementClick(driver, By.xpath(consultMap.get("ReleaseYesElement")));
             //切换到已发布tab
             Thread.sleep(500);
             seleniumUtil.elementClick(driver, By.xpath(consultMap.get("ReleasePublishElement")));
             //循环获取列表标题值 判断已发布里是否包含刚发布的咨询
             for (int i = 1; i < 21; i++) {
                 //System.out.println(i);
                 if(seleniumUtil.elementIsExitBaseByTime(driver,By.xpath("//*[@id=\"pane-published\"]/section/section[1]/div[2]/div[3]/table/tbody/tr["+i+"]/td[2]/div/a"),3)){
                     if(seleniumUtil.elementTextIsContains(driver,By.xpath("//*[@id=\"pane-published\"]/section/section[1]/div[2]/div[3]/table/tbody/tr["+i+"]/td[2]/div/a"),"自动化")){
                         releaseNum=i;
                         return true;
                     }
                 }else{
                     break;
                 }
             }
         } catch (InterruptedException e) {
             e.printStackTrace();
         }
         return false;
     }

    /**
     * 撤销发布咨询测试用例
     * @param driver
     * @return
     */
    public  boolean cancelReleaseConsult(WebDriver driver){
        try {
            //查到撤销发布的行数
            seleniumUtil.elementClick(driver, By.xpath("//*[@id=\"pane-published\"]/section/section[1]/div[2]/div[5]/div[2]/table/tbody/tr["+releaseNum+"]/td[1]/div/label/span/span"));
            Thread.sleep(500);
            //点击撤销发布
            seleniumUtil.elementClick(driver, By.xpath(consultMap.get("CancelReleaseOptionElement")));
            Thread.sleep(500);
            //点击确定
            seleniumUtil.elementClick(driver, By.xpath(consultMap.get("ReleaseYesElement")));
            Thread.sleep(500);
            return true;
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        return false;
    }

    /**
     * 定时发布咨询测试
     * @param driver
     * @return
     */
    public boolean timeReleaseConsult(WebDriver driver){

        try {
            //选中定时发布的咨询
            Thread.sleep(500);
            //未发布 里点击第一行
            seleniumUtil.elementClick(driver, By.xpath(consultMap.get("NoPublishFirstElement")));
            Thread.sleep(500);
            //点击定时发布
            seleniumUtil.elementClick(driver, By.xpath(consultMap.get("TimeReleaseElement")));
            Thread.sleep(500);
            //选择日期时间
            seleniumUtil.elementClick(driver, By.className(consultMap.get("ChooseDateAndTimeElement")));
            Thread.sleep(500);
            //获取当前日期
            String currentDate = DateUtils.getCurrentDate();
            //选择日期框输入 工具类获取到的当前日期
            seleniumUtil.elementClickAndClearAndInput(driver, By.xpath(consultMap.get("ChooseDateElement")),currentDate);
            //获取当前时间往后的秒钟
            String currentTime = DateUtils.getCurrentAfterTime(8);
            seleniumUtil.elementClickAndClearAndInput(driver, By.xpath(consultMap.get("ChooseTimeElement")),currentTime);
            //点击时间框确定
            Thread.sleep(200);
            seleniumUtil.elementClick(driver, By.xpath(consultMap.get("ChooseTimeYesElement")));
            seleniumUtil.elementClick(driver, By.xpath(consultMap.get("ChooseYesElement")));
            Thread.sleep(300);
            //提示框点击确定
            seleniumUtil.elementClick(driver, By.xpath(consultMap.get("ChooseTitleYesElement")));
            Thread.sleep(5000);
            //刷新
            seleniumUtil.refreshPage(driver);
            Thread.sleep(1500);
            //切换到已经发布
            seleniumUtil.elementClick(driver, By.xpath(consultMap.get("ChoosePublishElement")));
            Thread.sleep(1000);
            //对比时间判断是否定时发布
            String publishTimeElement = seleniumUtil.getElementText(driver, By.xpath(consultMap.get("PublishTimeElement")));
            if(publishTimeElement.contains(currentTime)){
                return true;
            }
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        return false;
    }

    /**
     * 置顶操作
     * @return
     */
    public boolean placedTopConsult(WebDriver driver){

        try {
            //选中当前第一行
            Thread.sleep(500);
            seleniumUtil.elementClick(driver, By.xpath(consultMap.get("PlacedTopFirstElement")));
            //点击置顶
            Thread.sleep(500);
            seleniumUtil.elementClick(driver, By.xpath(consultMap.get("PlacedTopButtonElement")));
            //点击确定
            Thread.sleep(500);
            seleniumUtil.elementClick(driver, By.xpath(consultMap.get("ChooseTitleYesElement")));
            Thread.sleep(200);
            //判断已置顶标签是否存在
            if(seleniumUtil.elementTextIsContains(driver,By.xpath(consultMap.get("TopTabElement")),"已置顶")){
                return true;
            }
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

        return false;
    }

}
